package goodenglish.domain.service.analysis;

import goodenglish.domain.model.subt.SubtSentence;
import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.DateUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SrtAnalyzer extends ImportAnalyzer {
	public SrtAnalyzer(final File file) {
		super(file);
	}

	/**
	 * 从字幕串里分析出句子
	 * 
	 * @param subtStr
	 * @param subt
	 */
	@Override
	public List<SubtSentence> analysisSentens() {
		List<SubtSentence> sentences = new ArrayList<SubtSentence>();
		String[] strs = subtStr.split(Config.LINE_SPLITER);
		int index = 1;
		int i = 1;
		SubtSentence sentence = null;
		for (final String str : strs) {
			if (str == null || str.isEmpty()) {
				continue;
			}
			if (str.equals("" + index)) {
				sentence = new SubtSentence();
				sentences.add(sentence);
				index++;
				i = 1;
			} else {
				if (i == 1) {
					sentence.setStimeStr(str.split(Config.TIME_SPLITER)[0].trim());
					sentence.setEtimeStr(str.split(Config.TIME_SPLITER)[1].trim());
					sentence.setStime(DateUtil.getMillisFromTimeStr(sentence.getStimeStr()));
					sentence.setEtime(DateUtil.getMillisFromTimeStr(sentence.getEtimeStr()));
				}
				if (i == 2) {
					sentence.setChStr(str);
				}
				if (i == 3) {
					sentence.setEnStr(str);
				}
				i++;

			}

		}
		return sentences;
	}

}
